/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package HumanCentredSystemDesign;

import java.util.ArrayList;
import java.util.Date;


/**
 *
 * @author Martin and Tim
 */
public class Book extends Item {
    
    private String author;
    
    public Book(){}

    public Book(String _isbn,String _author, Date _publicationDate, String _publisher, String _title){
        this.itemIdentifier = _isbn;
        this.author = _author;
        this.publicationDate = _publicationDate;
        this.publisher = _publisher;
        this.title = _title;
        this.type = "Book"; // Is this needed?
        
        
        
    } 
    
    @Override
    public String getAuthor(){
        return this.author;
    }
    public void setAuthor(String _author){
        this.author = _author;
    }
    
    @Override
    public String toString(){
     return this.itemIdentifier +" "+ this.title +" "+author+" "+ this.publisher+" "+ publicationDate;
    }
    
    public String toStringCopies(){
    String sb = new String();
         for (Copy s : this.getItemCopies())
                {
                    sb = sb +(s.toString());
                    sb = sb + ("\n");
                }
         return sb;
        
    }
    
    @Override
    public boolean isReserved(){
        ArrayList<Reservation> listOfReservations = new ArrayList<Reservation>();
        Connect myConn = new Connect();
        myConn.establishConnection();
        
        listOfReservations = myConn.getReservationsFromDatabase(this.itemIdentifier, "");
        myConn.closeConnection();
        
        return ! listOfReservations.isEmpty();
        
    }
    
    @Override
    public Borrower getFirstReserver(){
        
        Date myDate = new Date();
        String myBorrowerID = null;
        Borrower myBorrower = new Borrower();
        ArrayList<Reservation> listOfReservations = new ArrayList<Reservation>();
        Connect myConn = new Connect();
        myConn.establishConnection();
        
        listOfReservations = myConn.getReservationsFromDatabase(this.itemIdentifier, "");
        myConn.closeConnection();
        
        for ( Reservation r : listOfReservations)
        {
            if ( r.getReservationDate().before(myDate)){
                myDate = r.getReservationDate();
                myBorrowerID = r.getlibraryMemberID();
            }
        }
        
        LibraryCirculations lc = new LibraryCirculations();
        myBorrower = lc.getBorrower(myBorrowerID, "", "", "").get(0);
        
        
        
        return myBorrower;
    }
    
    @Override
    public ArrayList<Copy> getItemCopies(){
        ArrayList<Copy> listOfCopies = new ArrayList<Copy>();
        Connect myConn = new Connect();
        myConn.establishConnection();
        
        listOfCopies = myConn.getItemCopiesFromDatabase("%",this.itemIdentifier );
        myConn.closeConnection();
        return listOfCopies;
    }
    
    
}
